var prefix = jsctx + "/enterpriseMember";

var enterpriseSubmember = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listEnterpriseSubmember",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                sidePagination: "server",
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        mobile: $("#mobile").val().trim(),
                        belongEnterpriseId: $("#belongEnterpriseId").val().trim()
                    };
                },
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },

                    {field: 'nickname', title: '昵称', align: 'center', width: '100'},
                    {field: 'mobile', title: '手机号', align: 'center', width: '100'},
                    {field: 'createdTime', title: '审核时间', align: 'center', width: '100'},
                    {field: 'enterpriseAccount', title: '企业余额', align: 'center', width: '100'},
                    {
                        field: 'status',
                        title: '状态',
                        width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            if (row.status == '0') {
                                return '<span class="label label-success">正常</span>';
                            } else if (row.status == '2') {
                                return '<span class="label label-danger">冻结</span>';
                            }
                        }
                    },
                    {
                        field: 'chargeStatus',
                        title: '充电状态',
                        width: '100', align: 'center',
                        class: '',
                        formatter: function (value, row, index) {
                            if (row.chargeStatus == '1') {
                                return '<span class="label label-success">充电中</span>';
                            } else if (row.chargeStatus == '0') {
                                return '<span class="label label-danger">不在充电</span>';
                            }
                        }
                    },
                    {
                        title: '操作',
                        field: 'orderId',
                        width: '300', align: 'center',
                        formatter: function (value, row, index) {
                            var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="订单列表" onclick="enterpriseSubmember.submemberOrder(\''
                                + row.memberId + '\')">订单列表</a> ';

                            var b = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="冻结" onclick="enterpriseSubmember.changeStatus(\''
                                + row.memberId + '\')">冻结</a> ';

                            var h = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="恢复" onclick="enterpriseSubmember.changeStatus(\''
                                + row.memberId + '\')">恢复</a> ';

                            // if (row.status == '0') {
                            //     var b = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="冻结" onclick="enterpriseSubmember.changeStatus(\''
                            //         + row.memberId + '\')">冻结</a> ';
                            // } else if (row.status == '2') {
                            //     var b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="恢复" onclick="enterpriseSubmember.changeStatus(\''
                            //         + row.memberId + '\')">恢复</a> ';
                            // }

                            var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="注销" onclick="enterpriseSubmember.cancel(\''
                                + row.memberId + '\')">注销</a> ';

                            var d = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="共享余额" onclick="enterpriseSubmember.addSubmemberShare(\''
                                + row.memberId + '\')">共享余额</a> ';

                            var e = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="分配余额" onclick="enterpriseSubmember.editAmount(\''
                                + row.memberId + '\')">分配余额</a> ';

                            var f = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="余额" onclick="enterpriseSubmember.subMemberAmountRecord(\''
                                + row.memberId + '\')">余额</a> ';

                            var g = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="回收金额" onclick="enterpriseSubmember.recoveredAmount(\''
                                + row.memberId + '\')">回收金额</a> ';

                            if (row.chargeStatus == '1'){
                                return a + d + e + f;
                            } else if (row.chargeStatus == '0'){
                                if (row.status == '0'){
                                    return a + b + c + d + e + f + g;
                                } else if (row.status == '2') {
                                    return a + c + d + e + f + g + h;
                                }
                            }

                            // return a + b + c + d + e + f + g;
                            // return a + b + c + e + f + g;
                        }
                    }
                ]
            });
    };

    function submemberOrder(memberId) {
        location.href = prefix + "/submemberOrder/" + memberId;
    }

    function addSubmemberShare(memberId) {
        location.href = prefix + "/addSubmemberShare/" + memberId;
    }

    function editAmount(memberId) {
        location.href = prefix + "/editAmount/" + memberId;
    }

    function changeStatus(memberId) {
        layer.confirm('确定要变更状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateStatus",
                type: "post",
                data: {
                    'memberId': memberId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function cancel(memberId) {
        layer.confirm('确定要注销选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/cancel",
                type: "post",
                data: {
                    'memberId': memberId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("注销成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function subMemberAmountRecord(memberId) {
        location.href = prefix + "/subMemberAmountRecord/" + memberId;
    }

    function recoveredAmount(memberId) {
        layer.confirm('确定要回收金额？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/recoveredAmount",
                type: "post",
                data: {
                    'memberId': memberId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("回收成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function exportEnterpriseSubmember() {
        $.fileDownload(prefix + "/exportEnterpriseSubmember", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                belongEnterpriseId: $("#belongEnterpriseId").val().trim(),
                mobile: $("#mobile").val().trim(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()

            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }



    return {
        init: init,
        reLoad: reLoad,
        submemberOrder: submemberOrder,
        changeStatus: changeStatus,
        cancel: cancel,
        addSubmemberShare: addSubmemberShare,
        editAmount: editAmount,
        subMemberAmountRecord: subMemberAmountRecord,
        recoveredAmount: recoveredAmount,
        exportEnterpriseSubmember:exportEnterpriseSubmember
    };
})();
